from typing import List
import traceback
import sys

from pymodels.models.dpr import tinybertVectorizer
from pymodels.utils.BookReader import TxtBookReader

from ..basic import BasicComprehensionPlugin


def test_comprehension_inference(comprehensionplugin,
                                 question: str, bookname: str, **kw) -> bool:
    """
    用于测试一个阅读理解插件是股沟能够正常运行。
    :param comprehensionplugin: 插件的名称。
    :param question: 问题。
    :param bookname: 书籍路经。
    :return: 返回布尔值，表明是否成果。
    """
    print("=+" * 6, "加载阅读理解插件", "+=" * 6)
    try:
        o = comprehensionplugin()  # noqa
    except Exception:
        print("=+" * 6, "加载阅读理解插件失败", "+=" * 6)
        traceback.print_exc(file=sys.stdout)
        return False
    print("=+" * 6, "加载推理环境", "+=" * 6)
    dpr = tinybertVectorizer()
    reader = TxtBookReader(verctorizer=dpr)
    reader.readbook(bookname)
    _, context = reader.search(question, k=3)
    print("context", context)
    print("=+" * 6, "进行推理", "+=" * 6)
    try:
        if hasattr(o, "wake"):
            o.wake()
        out = o.inference(context, question, **kw)
    except Exception:
        print("=+" * 6, "推理失败", "+=" * 6)
        traceback.print_exc(file=sys.stdout)
        return False
    print("=+" * 6, "推理成功，以下是结果：", "+=" * 6)
    print(out)
    print("=+" * 6, "输出完毕", "+=" * 6)
